using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MouseBehavior : MonoBehaviour {

	public GameObject whiteLight;
	public GameObject yellowLight;

	public GameObject[] allFurnitures;

	Camera mainCamera;

	GameObject tempObject;
	GameObject furnitures;

	Vector3 rotEulerAngle = new Vector3 (30, 180, 0);
	Vector3 normalVector = new Vector3 (0, 3f, 5);
	Vector3 detailVector = new Vector3 (0, 2f, 2.5f);
	Vector3 tempV;
	Vector3 tempMousePos;
	Vector3 deltaMousePos;
	Vector3 clickPos;

	bool cameraChange = false;
	bool detailHold = false;
	bool normalHold = false;
	bool detail = false;

	// Use this for initialization
	void Start () {
		mainCamera = Camera.main.camera;
		mainCamera.transform.position = normalVector;

		furnitures = GameObject.Find("Furnitures");

		if (furnitures == null) {
			Debug.Log("furnitures is null");
		}

		allFurnitures = GameObject.FindGameObjectsWithTag ("furniture") ;
	}
	
	// Update is called once per frame
	void Update () {

		if (Input.GetMouseButtonDown (0) && detail == false) {

			if(theCorrectObject() == true){
				tempMousePos = Input.mousePosition;
				clickPos = Input.mousePosition;
				moveFurniture();
			}

		} else if(Input.GetMouseButtonUp (0) && detail == false){
			if(theCorrectObject() == true && clickPos == Input.mousePosition){
				detail = true;
			}
		} else if (Input.GetMouseButtonDown (0) && detail == true) {

			if(theCorrectObject() == true){
				tempMousePos = Input.mousePosition;

				detailModel();
			}


		} else {
			objectScale();
			
		}
		
		if(detail) {
			if(!cameraChange){
				showOneFurniture (tempObject);

				pushFurnitureToCenter();

				tempV = Vector3.Lerp (normalVector, detailVector, Time.time * 0.7f);
			
				mainCamera.transform.position = tempV;
				cameraChange = true;
			}
		}
		if(Input.GetMouseButtonUp(0)){
			detailHold = false;
			if(normalHold == true){

				normalHold = false;
			}
		}
		if (tempObject != null) {
						RotateOrMoveFurniture (tempObject);
		}
	}

	void LaterUpdate(){

	}

	bool theCorrectObject(){
		
		Ray mouseRay = mainCamera.ScreenPointToRay (Input.mousePosition);
		
		RaycastHit rayHit;
		if (Physics.Raycast (mouseRay, out rayHit)) {
			if (rayHit.collider.gameObject == null) {
				return false;
			}
			
			if (rayHit.collider.gameObject.tag == "furniture") {
				tempObject = rayHit.collider.gameObject;
				return true;
			} else {
				return false;
			}
		}
		return false;
	}
	
	bool objectScale(){
		if (detail == true)
						return false;

		Ray mouseRay = mainCamera.ScreenPointToRay (Input.mousePosition);
		
		RaycastHit rayHit;
		if (Physics.Raycast (mouseRay, out rayHit)) {
			Debug.Log("ray object name :"+rayHit.collider.gameObject.name);
			if(rayHit.collider.gameObject.tag == "furniture"){
				if(tempObject != rayHit.collider.gameObject){
					
					rayHit.collider.gameObject.transform.localScale = 1.2f*Vector3.one;
					
					if(tempObject != null){
						tempObject.gameObject.transform.localScale = Vector3.one;
					}
					tempObject = rayHit.collider.gameObject;

				}
				return true;
			}else{
				if(tempObject != null){
					tempObject.gameObject.transform.localScale = Vector3.one;
					tempObject = null;
				}
			}
		}
		return false;
	}

	void detailModel(){

		Debug.Log("detail model");
		detailHold = true;
	}

	float pushX;
	void pushFurnitureToCenter(){
		pushX = tempObject.transform.localPosition.x;

		furnitures.transform.position = new Vector3 (-pushX, 0, 0);
	}

	void moveFurniture(){
		Debug.Log("move furniture");
		normalHold = true;
	}

	float rotX;
	float rotY;

	float moveX;
	void RotateOrMoveFurniture(GameObject holdObject){

		deltaMousePos = Input.mousePosition - tempMousePos;

		if (holdObject.tag != "furniture")
			return;

		if (detailHold) {

			rotX = deltaMousePos.x * 10/36;
			rotY = -deltaMousePos.y * 10/36;

			mainCamera.transform.RotateAround (tempObject.transform.position, mainCamera.transform.up, rotX);
			mainCamera.transform.RotateAround (tempObject.transform.position, mainCamera.transform.right, rotY);

			tempMousePos = Input.mousePosition;
		}

		if (normalHold) {

			moveX = deltaMousePos.x*0.01f;
			furnitures.transform.Translate(new Vector3(-moveX, 0, 0));

			tempMousePos = Input.mousePosition;


		}
	}

	void reset(){
		tempObject = null;
		mainCamera.transform.position = normalVector;
		mainCamera.transform.eulerAngles = rotEulerAngle;

		cameraChange = false;
		detailHold = false;
		normalHold = false;
		detail = false;

		showAllFurnitures ();
	}

	void changeLight(string str){
		if (str == "white") {
			whiteLight.SetActive(true);
			yellowLight.SetActive(false);
		} else {
			whiteLight.SetActive(false);
			yellowLight.SetActive(true);
		}
	}

	void showAllFurnitures(){
		foreach(GameObject furniture in allFurnitures){
			furniture.SetActive(true);
		}
	}

	void showOneFurniture(GameObject one){
		foreach(GameObject furniture in allFurnitures){
			furniture.SetActive(false);
		}

		one.SetActive (true);
	}

	//GUI
	void OnGUI(){

		if(detail)
			GUI.Label(new Rect(Screen.width/2, 40, 100, 30), "detail");
		else
			GUI.Label(new Rect(Screen.width/2, 40, 100, 30), "browse");

		if (GUI.Button (new Rect (30, Screen.height*4/5, 100, 30), "back")) {
			reset();
		}

		if (GUI.Button (new Rect (Screen.width * 2/ 5, Screen.height * 4 / 5, 100, 30), "white")) {
			changeLight("white");
		}
		
		if (GUI.Button (new Rect (Screen.width * 3 / 5, Screen.height * 4 / 5, 100, 30), "yellow")) {
			changeLight("yellow");
		}
	}
}
